package com.zhangxin.easyexcelutil.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.zhangxin.easyexcelutil.annotation.ExcelAnnotation;
import org.springframework.util.Assert;

import java.util.Objects;

/**
 * Created by zhangxin on 2021/1/30.
 */
public class ExcelConverter implements Converter {
    @Override
    public Class supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Object convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration){
        String cellDataStr = cellData.getStringValue();

        ExcelAnnotation annotation = excelContentProperty.getField().getAnnotation(ExcelAnnotation.class);
        String[] fromExcel = annotation.excel();
        String[] toJava = annotation.java();

        if (null!=fromExcel && null!=toJava && fromExcel.length>0 && toJava.length>0) {
            String value="";
            Assert.isTrue(fromExcel.length == toJava.length, "fromExcel 与 toJava 的长度必须相同");
            if(cellDataStr.contains(",")){
                for(String key :cellDataStr.split(",")){
                    for(int i=0;i<fromExcel.length;i++){
                        if(Objects.equals(fromExcel[i], key)){
                            value +=toJava[i]+",";
                        }
                    }
                }
                value = value.substring(0,value.length()-1);
            }else{
                for(int i=0;i<fromExcel.length;i++){
                    if(Objects.equals(fromExcel[i], cellDataStr)){
                        value =toJava[i];
                    }
                }
            }
            return value;
        }
        return "-";
    }

    @Override
    public CellData convertToExcelData(Object o, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration){
        if(Objects.isNull(o)) {
            return new CellData();
        }
        ExcelAnnotation annotation = excelContentProperty.getField().getAnnotation(ExcelAnnotation.class);
        String[] fromExcel = annotation.excel();
        String[] toJava = annotation.java();

        if (null!=fromExcel && null!=toJava && fromExcel.length>0 && toJava.length>0) {
            String value="";
            Assert.isTrue(fromExcel.length == toJava.length, "fromExcel 与 toJava 的长度必须相同");
            if(((String)o).contains(",")){
                for(String key :((String)o).split(",")){
                    for(int j=0;j<toJava.length;j++){
                        if(Objects.equals(toJava[j], key)){
                            value +=fromExcel[j]+",";
                        }
                    }
                }
                value = value.substring(0,value.length()-1);
            }else{
                for(int j=0;j<toJava.length;j++){
                    if(Objects.equals(toJava[j], o)){
                        value =fromExcel[j];
                    }
                }
            }
            return new CellData(value);
        }
        return new CellData();
    }
}
